<?php

namespace App\Http\Controllers\Admin;

use DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\AdminUser;
use App\Models\AdminGroup;

class AdminController extends Controller
{
	public function __construct()
	{
	    DB::enableQueryLog();
	}
    /**
     * 管理员列表
     *
     * @return mixed
     */
    public function adminList()
    {
        // $admins = DB::table('admin_user as u')->leftJoin('admin_group as g','u.groupid','=','g.id')->select('u.id,u.name,u.create_at,u.last_at,u.last_ip,g.title,g.msg')->get();
        $admins = DB::table('admin_user as u')->leftJoin('admin_group as g','u.groupid','=','g.id')->select('u.id','u.name','u.create_at','u.last_at','u.last_ip','u.status','g.title','g.msg')->get();
     //    $sql = DB::getQueryLog();
	    // $query = end($sql);
	    // dd($admins);
        return view('admin.admin.adminList',['admins'=>$admins]);
    }
    public function addAdmin(Request $request)
    {
    	if($request->isMethod('post')){
    		$msg = '';
    		$name =  $request->name;
            $groupid = $request->groupid;
            $password1 = $request->password;
            $password2 = $request->password2;
            $only = AdminUser::where('name','=',$name)->first();
            // dd($only);
            if(!empty($only)){
            	return '账号已存在';
            }
            if($password1 !== $password2){
            	return '两次密码不一致！';
            }
            $password = bcrypt($password1);
            $status = 1;
            $create_at = time();
            $params = compact('name','password','status','groupid','create_at');
            $row = AdminUser::create($params);
            if ($row) {
                return 1;
            } else {
                return '添加失败';
            }
    	}
        $groups = AdminGroup::get(['id','title'])->toArray();
        return view('admin.admin.addAdmin',['groups'=>$groups]);
    }
    public function editAdmin(Request $request)
    {
        if($request->isMethod('post')){
    		$msg = '';
    		$uid = $request->id;
    		$name =  $request->name;
            $groupid = $request->groupid;
            $password1 = $request->password;
            $password2 = $request->password2;
            if($password1 !== $password2){
            	return '两次密码不一致！';
            }
            $password = bcrypt($password1);
            $update_at = time();
            $params = compact('name','password','groupid','update_at');
            $post = AdminUser::find($uid);
            if ($post->update($params)) {
                return 1;
            } else {
                return '修改失败';
            }
    	}
    	$id = $request->id;
    	$user = AdminUser::where('id','=',$id)->first(['id','name','groupid']);
        $groups = AdminGroup::get(['id','title'])->toArray();
        return view('admin.admin.editAdmin',['groups'=>$groups,'user'=>$user]);
    }

    public function delAdmin(Request $request)
    {
        $id = $request->id;
        $is = $request->is;
        // dd($is);
        $post = AdminUser::find($id);
        $post->status = $is;
        $post->update_at = time();
        $row = $post->save();
        if ($row) {
            $call = 1;
        } else {
            $call = '变更失败';
        }
        return $call;
    }



}
